﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using ET;
using Marx;

namespace Demo
{
    public class SendMessageTest : MonoBehaviour
    {
        public void OnButtonClick()
        {
            UnityEngine.Debug.Log("消息发送：1");
            var zone = Game.Scene.Children.First().Value as Scene;
            UnityEngine.Debug.Log("消息发送：2");
            var component = zone.GetComponent<NetTcpComponent>();
            UnityEngine.Debug.Log("消息发送：3");
            var session = component.Create(NetworkHelper.ToIPEndPoint("127.0.0.1", 20001));
            UnityEngine.Debug.Log("消息发送：4");
            SendMessage(session).Coroutine();
        }

        private async ETTask SendMessage(Session session)
        {
            try
            {
                UnityEngine.Debug.Log("消息发送：");
                var resp = await session.Call(new C2GTestReqeust() { Content = "Hellow world" }) as C2GTestResponse;
                UnityEngine.Debug.Log("消息返回：" + resp.Content);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
    }
}
